package com.henry.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.StandardServletEnvironment;

import java.util.Iterator;

/**
 * @author Henry
 * @date 2020/6/25 14:18
 */
@RestController
public class EnvController {
    @Autowired
    private Environment env;

    @RequestMapping("/env")
    public String env(){
        StandardServletEnvironment environment = (StandardServletEnvironment) env;
        MutablePropertySources propertySources = environment.getPropertySources();
        Iterator<PropertySource<?>> iterator = propertySources.iterator();
        while (iterator.hasNext()) {
            PropertySource<?> propertySource = iterator.next();
            if (propertySource instanceof MapPropertySource) {
                MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
                String[] propertyNames = mapPropertySource.getPropertyNames();
                for (String s : propertyNames) {
                    System.out.println(s+":"+mapPropertySource.getProperty(s));
                }
            }
        }
        return env.getProperty("user.name")+"-"+env.getProperty("user.age");
    }
}
